import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../generated/l10n.dart';
import '../../res/button_style.dart';
import '../../res/text_style.dart';

/// @description :公共通用弹窗

typedef InputDialogCallback = void Function(String inputResult);

class InputDialog extends StatelessWidget {
  ///标题
  String title = '';

  ///内容
  String content = '';

  int inputLength = 10;

  ///回调事件
  InputDialogCallback? callback;

  TextInputType inputType;

  var _inputController = TextEditingController();

  InputDialog(
      {Key? key,
      this.title = '',
      this.content = '',
      this.inputLength = 10,
      this.callback,
      this.inputType = TextInputType.text})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return AnimatedPadding(
      padding: EdgeInsets.only(
        // 下面这一行是重点
        bottom: MediaQuery.of(context).viewInsets.bottom,
      ),
      duration: Duration.zero,
      child: Material(
        ///透明样式
        type: MaterialType.transparency,
        child: Container(
          alignment: Alignment.bottomCenter,

          ///背景及内容、边距、圆角等，必须包裹在ListView中
          child: SingleChildScrollView(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Container(),
                Container(
                  width: double.infinity,
                  margin:
                      EdgeInsets.symmetric(horizontal: 15.w, vertical: 15.h),
                  child: Column(
                    children: [
                      Container(
                        decoration: const ShapeDecoration(
                          color: Colors.white,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.all(
                              Radius.circular(20.0),
                            ),
                          ),
                        ),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: <Widget>[
                            ///标题、内容
                            Text(
                              title,
                              style: TextStyle(
                                  color: Colors.black, fontSize: 16.sp),
                            ).marginOnly(top: 16.h),
                            Padding(
                              padding: EdgeInsets.symmetric(
                                  horizontal: 20.w, vertical: 16.h),
                              child: TextFormField(
                                controller: _inputController,
                                autofocus: true,
                                keyboardType: inputType,

                                style:
                                    TextStyles.textStyle(Colors.black, 16.sp),

                                inputFormatters: <TextInputFormatter>[
                                  LengthLimitingTextInputFormatter(
                                      inputLength), //限制长度
                                ],
                                // autofocus: true,
                                // textInputAction: TextInputAction.done,
                                // enabled: false,
                                maxLines: null,
                                //显示'下一步'
                                decoration: InputDecoration(
                                  // hintText: content,
                                  filled: true,
                                  fillColor: const Color(0xFFF6F6F6),
                                  contentPadding: EdgeInsets.fromLTRB(
                                      20.w, 10.h, 20.w, 10.h),
                                  border: OutlineInputBorder(
                                      borderRadius:
                                          BorderRadius.circular(52.0) //圆角大小
                                      ),
                                  focusedBorder: OutlineInputBorder(
                                      borderRadius: BorderRadius.circular(52.0),
                                      borderSide: const BorderSide(
                                        width: 1,
                                        color: Color(0xFFF6F6F6),
                                      )),
                                  enabledBorder: OutlineInputBorder(
                                    borderRadius: BorderRadius.circular(52.0),
                                    borderSide: const BorderSide(
                                        width: 1, color: Color(0xFFF6F6F6)),
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                      buildBtnSection(),
                    ],
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget buildBtnSection() {
    return Container(
      width: double.infinity,
      margin: EdgeInsets.only(top: 12.h, bottom: 20.h),
      height: 55.h,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(20.w)),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          Expanded(
              flex: 1,
              child: TextButton(
                onPressed: () {
                  Get.back();
                },
                style: ButtonStyles.getNoShapeStyle(),
                child: Container(
                  alignment: Alignment.center,
                  height: 60,
                  child: Text(
                    S.current.cancel,
                    style: TextStyles.textStyle(const Color(0xFFC5C5C5), 16.sp),
                  ),
                ),
              )),

          Expanded(
              flex: 1,
              child: TextButton(
                onPressed: () {
                  FocusScope.of(Get.context!).unfocus();
                  Get.back();
                  if (callback != null) {
                    callback!(_inputController.text);
                  }
                },
                style: ButtonStyles.getNoShapeStyle(),
                child: Container(
                  alignment: Alignment.center,
                  height: 60,
                  child: Text(
                    S.current.confirm,
                    style: TextStyles.textStyle(const Color(0xFF1877FF), 16.sp),
                  ),
                ),
              )),
          // ),
        ],
      ),
    );
  }
}
